package lastSymbol

import (
	"fmt"
	"github.com/Jeffail/gabs"
	kjson "github.com/heyuanlong/go-tools/json"
	"github.com/parnurzeal/gorequest"
	"log"
	"strconv"
	"time"
)


type OkexPrice struct {
	httpDomain string
}

func NewOkexPriceStruct() *OkexPrice {
	obj := &OkexPrice{}
	obj.init()
	return obj
}

func (ts *OkexPrice) init() {
	ts.httpDomain = "https://www.okex.me/"
}

func (ts *OkexPrice) GetPriceUsdt(name string) (float64, error) {
	symbol := name + "-USDT"
	uri := ts.httpDomain + "api/spot/v3/instruments/"+ symbol +"/ticker"
	request := gorequest.New().Timeout(time.Duration(2) * time.Second)
	_, body, errs := request.Get(uri).End()
	if errs != nil {
		log.Println(errs)
		return 0, errs[0]
	}

	jsonParsed, err := gabs.ParseJSON([]byte(body))
	if err != nil {
		err = fmt.Errorf("get ok lasted price failed, err:%v\n", err.Error())
		return 0, err
	}
	s, err := kjson.GabsGetJsonString(jsonParsed, "last")
	if err != nil {
		log.Println(body, err)
		return 0, err
	}
	retv, errx := strconv.ParseFloat(s, 64)
	return retv, errx
}

func (ts *OkexPrice) GetPriceRmb(name string) (float64, error) {
	return 0, nil
}